var imgs = new Array();
	imgs[1] = 'Submit.png';
	imgs[2] = 'backup.png';
	imgs[3] = 'Delete.png';
var spanStatus = new Array();
	spanStatus[1] = 'inscrits';
	spanStatus[2] = 'backup';
	spanStatus[3] = 'absents';


    
function ShowListe(origine) {
	var source = document.getElementById(origine);
	var coord = source.parentElement.getBoundingClientRect();
	var dateEvent = document.getElementById('dateEvent');
	dateEvent.value = origine;
	x = coord.left+1;
	y = coord.bottom;
	var liste = document.getElementById('typeEvent');
	liste.style.left = x+'px';
	liste.style.top = y+'px';
}

function showAction(origine, clanEvent, clan) {
	var source = document.getElementById(origine);
	var coord = source.getBoundingClientRect();
	x = coord.left+1;
	y = coord.bottom;
	var delEvent = 'block';
	if (clanEvent != clan) {
		delEvent = 'none';
	}
	var liste = document.getElementById('agendaAction');
	var liens = liste.children[0].children;
	//liste.innerHTML=liste.innerHTML.split('#EVENTID#').join(origine);
	for(i=0; i <liens.length; i++) {
		var virg = liens[i].children[0].href.indexOf(',');
		var eId = liens[i].children[0].href.lastIndexOf('/') ;
		if (virg != -1) {
			var debut = liens[i].children[0].href.substr(0, virg);
			liens[i].children[0].href = debut+','+origine+')';	
		}
		else if (eId != -1) {
			var debut = liens[i].children[0].href.substr(0, eId);
			liens[i].children[0].href = debut+'/'+origine;	
		}
		if(debut.substr(-1,1) == 5) {
			liens[i].style.display = delEvent;
		}
	}	
	liste.style.left = x+'px';
	liste.style.top = y+'px';
}

function cacherListe(e) {
	var liste = document.getElementById('typeEvent');
	if (liste != null) {
		var target = e.target || e.srcElement;
		if(target.tagName == 'A' && (target.parentElement.tagName == 'DIV' && target.parentElement.className=='jour')) {
			return true;
		}
		else {
			liste.style.left = '-999em';
		}
	}
}

function executerAction(action_id, event_id){
	
	var xhr=null;
	var wotHub = 'http://'+document.domain;

	if (document.domain == 'localhost') {
		wotHub = 'http://'+document.domain+'/wothub';
	}
    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
    xhr.onreadystatechange = function() { traiterReponseAgenda(xhr); };
 
    //on appelle le fichier reponse.txt
    tempo();
    xhr.open("GET", wotHub+"/Agenda/traiterActionXHR/"+action_id+'/'+event_id);
    //xhr.open("GET", "../test.php");
    xhr.send(null);
	
}

function traiterReponseAgenda(xhr)
{
	if (xhr.readyState==4) 
    {
    	var docXML= xhr.responseXML;
    	var items = docXML.getElementsByTagName("action") ;
    	var ids = docXML.getElementsByTagName("id") ;
    	var target = document.getElementById(ids[0].textContent);
    	if (items[0].textContent == 5){
    		target.parentElement.style.display = 'none';
    	}
    	else {
    		if (target != null ) {
    			if(target.parentElement.children.length > 1) {
        			changeInscriptionValue(target, items[0].textContent, true, getLastStatus(target.parentElement.children[1]));
        			target.parentElement.children[1].src = '../images/'+imgs[items[0].textContent];
        		}
        		else {
        			changeInscriptionValue(target, items[0].textContent, false, 0);
        			target.parentElement.innerHTML+='<img src="../images/'+imgs[items[0].textContent]+'" />';
        		}
    		} 
    	}
    	traiterActionCalendrier(ids[0].textContent, items[0].textContent);
    	tempo();
    	//on fait juste une boucle sur chaque element "donnee" trouvé
    }
}

function traiterActionCalendrier(target, actionId) {
	var calEvent = document.getElementById('cal'+target);
	if (calEvent == null) {
		return false;
	}
	switch (actionId) {
	case '5':
		calEvent.parentElement.parentElement.style.display = 'none';
	break;
	
	default:
		if (calEvent.children.length == 3) {
			var status = calEvent.children[2];
			changeInscriptionValue(calEvent, actionId, true, getLastStatus(status));
			status.src = status.src.substr(0, status.src.lastIndexOf('/')+1)+imgs[actionId];
		}
		else {
			var status = document.createElement(('IMG'));
			var imgsrc = 'http://'+document.domain;  
			if (document.domain == 'localhost') {
				imgsrc = 'http://'+document.domain+'/wothub';
			}
			status.src = imgsrc+'/images/'+imgs[actionId];
			calEvent.appendChild(status);
			changeInscriptionValue(calEvent, actionId, false, 0);
		}
	break;
	}
}

function getLastStatus(target) {
	var img = target.src.substr(target.src.lastIndexOf('/')+1)
	for (i=1; i<4; i++) {
		if (img == imgs[i]) {
			return i;
		}
	}
	return false;
}

function changeInscriptionValue(target, status, dejaInscrit, oldValue) {
	if (dejaInscrit == true) {
		target.children[1].children[oldValue-1].innerText =  Number(target.children[1].children[oldValue-1].innerText)-1; 
		target.children[1].children[status-1].innerText = Number(target.children[1].children[status-1].innerText)+1;
	}
	else {
		target.children[1].children[status-1].innerText = Number(target.children[1].children[status-1].innerText)+1;		
	}
}

function cacherAction(e) {
	var liste = document.getElementById('agendaAction');
	if (liste != null) {
		var target = e.target || e.srcElement;
		if(target.tagName == 'A' && (target.parentElement.parentElement.tagName == 'UL' && target.parentElement.parentElement.className=='Event')) {
			return true;
		}
		else {
			liste.style.left = '-999em';
		}
	}
}

if (window.addEventListener) {
	window.addEventListener("click", cacherListe);
	window.addEventListener("click", cacherAction);
}
else if (window.attachEvent) {
	window.addEventListener("onclick", cacherListe);
	window.addEventListener("onclick", cacherAction);
}

function enregistrerEvent(id, eslid) {
	tempo();
	var tEvent = document.getElementById('tEvent');
	tEvent.value = id+'_'+eslid;
	document.forms[0].submit();
}
